package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    JedisPool jedisPool;

    @Reference
    SetmealService setmealService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile")MultipartFile imgFile){
       try {
           //获取文件名
           String name=imgFile.getOriginalFilename();
           //获取文件后缀
           String perfix= name.substring(name.lastIndexOf("."));
           String Filename= UUID.randomUUID().toString().toUpperCase()+perfix;
           //上传到七牛云
           QiniuUtils.upload2Qiniu(imgFile.getBytes(),Filename);
            //保存图片名称到redis
           jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,Filename);
           return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,Filename);
       }catch (Exception e){
           e.printStackTrace();
           return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
       }
    }

    @RequestMapping("/add")
    public  Result  add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findpage")
    public PageResult findpage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult= setmealService.findpage(
                queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("/findById")
    public  Result findById(Integer id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true,"",setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"");
        }
    }

    @RequestMapping("/selectCheckgroupIdsById")
    public List<Integer> selectCheckgroupIdsById(int id){
       List<Integer> list= setmealService.selectCheckgroupIdsById(id);
       return list;
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            //先根据ID查询之前的图片
            String name = setmealService.findImgById(setmeal.getId());
            //删除redis中存放的跟数据库相关的Filename
            Jedis jedis = jedisPool.getResource();
            jedis.srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,name);

            setmealService.edit(setmeal,checkgroupIds);
            jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());

            return new Result(true,"编辑套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑套餐失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            String name = setmealService.findImgById(id);
            Jedis jedis = jedisPool.getResource();
            jedis.srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,name);
            setmealService.delete(id);
            return new Result(true,"删除套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除套餐失败");
        }
    }

}
